package com.help10000.rms.ui.activitys

import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import androidx.recyclerview.widget.LinearLayoutManager
import com.amir.common.beans.ProDuctData
import com.amir.common.beans.Prod
import com.amir.common.utils.IMEUtil
import com.amir.common.weight.DuplicateClickProxy
import com.amir.common.weight.SpaceItemDecoration
import com.help10000.rms.R
import com.help10000.rms.mvp.presenter.SelectProductPresenter
import com.help10000.rms.mvp.view.SelectProductView
import com.help10000.rms.ui.adapter.SelectProductAdapter
import com.help10000.rms.ui.base.BaseActivity
import kotlinx.android.synthetic.main.activity_select_product_activity.*

class SelectMaterialActivity : BaseActivity<SelectProductPresenter, SelectProductView>(),
    SelectProductView {
    var mWareId: String? = ""
    var RepairId: String? = ""
    var searchKey: String? = ""
    val prodList = mutableListOf<Prod>()
    var selectProductAdapter: SelectProductAdapter? = null
    override fun getlayoutId(): Int {
        return R.layout.activity_select_product_activity
    }

    override fun initView() {
        initRefreshLayout(refresh)
    }

    override fun initData(savedInstanceState: Bundle?) {
        this.mWareId = intent.getStringExtra("WareId")
        this.RepairId = intent.getStringExtra("RepairId")
        downOnRefresh()
        val linearLayoutManager = LinearLayoutManager(mContext)
        rv_list.layoutManager = linearLayoutManager
        selectProductAdapter =
            SelectProductAdapter(mContext, prodList, R.layout.item_select_product)
        rv_list.adapter = selectProductAdapter
        rv_list.addItemDecoration(SpaceItemDecoration(0, 10))
        selectProductAdapter?.setOnItemClickListener {
            val prod = prodList[it]
            var intent = Intent(mContext, AddMaterialActivity::class.java)
            intent.putExtra("data", prod)
            intent.putExtra("RepairId", RepairId)
            intent.putExtra("WareId", mWareId)
            startActivityForResult(intent, 1)
        }
    }

    override fun initListener() {
        super.initListener()
        tv_search.setOnClickListener(DuplicateClickProxy {
            downOnRefresh()
        })
        et_search1.setOnKeyListener { v, keyCode, event ->
            if (keyCode == KeyEvent.KEYCODE_ENTER && event.action == KeyEvent.ACTION_DOWN) {
                downOnRefresh()
                true

            }
            false

        }
    }

    override fun downOnRefresh() {
        mPresenter.PageNo = 1
        searchKey = et_search1.text.toString().trim()
        mPresenter.getData(mWareId, RepairId, searchKey)
        IMEUtil.hideIME(this, et_search1)
        super.downOnRefresh()
    }

    override fun loadMore() {
        super.loadMore()
        mPresenter.PageNo++
        searchKey = et_search1.text.toString().trim()
        mPresenter.getData(mWareId, RepairId, searchKey)
    }

    override fun createPresenter(): SelectProductPresenter {
        return SelectProductPresenter(this)
    }

    override fun dataSuccess(data: ProDuctData) {
        onRefreshFinish(true)
        onLoadMoreFinish(true)
        if (data.ProdList.isEmpty()) {
            onFinishNoMore()
            return
        }
        if (mPresenter.PageNo == 1) {
            prodList.clear()
            prodList.addAll(data.ProdList)
            selectProductAdapter?.notifyDataSetChanged()
            rv_list.scrollToPosition(0)
        } else {
            var oldCount = selectProductAdapter!!.itemCount;
            prodList.addAll(data.ProdList)
            selectProductAdapter?.notifyItemRangeInserted(
                oldCount,
                data.ProdList.size
            )
        }

    }
}
